package cn.edu.stu.syllabus.adapters;

import android.support.annotation.IntDef;
import android.support.v4.content.ContextCompat;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseViewHolder;

import java.lang.annotation.Retention;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.edu.stu.syllabus.R;
import cn.edu.stu.syllabus.application.SyllabusApplication;
import cn.edu.stu.syllabus.base.BaseQuickClickAdapter;
import cn.edu.stu.syllabus.repository.takeout.local.DishesEntry;
import cn.edu.stu.syllabus.datas.StickyState;
import cn.edu.stu.syllabus.datas.ThemeStyle;

import static java.lang.annotation.RetentionPolicy.SOURCE;

/**
 * Created by daidaijie on 2017/4/4.
 */

public class DishesAdapter extends BaseQuickClickAdapter<DishesEntry, DishesAdapter.ViewHolder> {

    private String mKeyWords;

    Map<DishesEntry, Integer> mBuyDishesMap;

    OnNumChangeListener mOnNumChangeListener;

    @ModeDef
    private int mMode;

    @Override
    protected ViewHolder createBaseViewHolder(View view) {
        return new ViewHolder(view);
    }

    public DishesAdapter(List<DishesEntry> data, @ModeDef int mode) {
        super(R.layout.item_dishes, data);
        mMode = mode;
        mKeyWords = "";
        mBuyDishesMap = new HashMap<>();
    }

    public void setKeyWords(String keyWords) {
        mKeyWords = keyWords;
    }

    @Override
    protected void convert(final ViewHolder helper, final DishesEntry item) {
        helper.mDishesPriceTextView.setText(item.getPrice());

        if (mMode == Mode.NORMAL) {
            helper.mDishesNameTextView.setText(item.getName());
        } else {
            if (mKeyWords.trim().isEmpty()) {
                helper.mDishesNameTextView.setText(item.getName());
            } else {
                SpannableStringBuilder style = new SpannableStringBuilder(item.getName());
                int index = item.getName().indexOf(mKeyWords);
                if (index != -1) {
                    style.setSpan(new ForegroundColorSpan(
                                    ContextCompat.getColor(SyllabusApplication.getContext(),
                                            ThemeStyle.getCurrentColorPrimaryLight())
                            ),
                            index, index + mKeyWords.length(), Spannable.SPAN_EXCLUSIVE_INCLUSIVE
                    );
                    helper.mDishesNameTextView.setText(style);
                }
            }
        }

        int position = helper.getAdapterPosition();

        if (mMode == Mode.NORMAL) {
            if (position == 0) {
                helper.mTvStickyHeaderView.setVisibility(View.VISIBLE);
                helper.mStickyTextView.setText(item.getSubMenuName());
                helper.itemView.setTag(StickyState.FIRST_STICKY_VIEW);
            } else {
                if (!TextUtils.equals(item.getSubMenuName(),
                        getData().get(position - 1).getSubMenuName())) {
                    helper.mTvStickyHeaderView.setVisibility(View.VISIBLE);
                    helper.mStickyTextView.setText(item.getSubMenuName());
                    helper.itemView.setTag(StickyState.HAS_STICKY_VIEW);
                } else {
                    helper.mTvStickyHeaderView.setVisibility(View.GONE);
                    helper.itemView.setTag(StickyState.NONE_STICKY_VIEW);
                }
            }
            helper.itemView.setContentDescription(item.getSubMenuPos() + "");
        } else {
            helper.mTvStickyHeaderView.setVisibility(View.GONE);
        }

        updateItem(helper, item);

        helper.mAddButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnNumChangeListener != null) {
                    mOnNumChangeListener.onAddNum(helper.mAddButton, item, helper.getAdapterPosition());
                    updateItem(helper, item);
                }
            }
        });

        helper.mMinusButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnNumChangeListener != null) {
                    mOnNumChangeListener.onReduceNum(helper.mMinusButton, item, helper.getAdapterPosition());
                    updateItem(helper, item);
                }
            }
        });


    }

    public Map<DishesEntry, Integer> getBuyDishesMap() {
        return mBuyDishesMap;
    }

    public void setBuyDishesMap(Map<DishesEntry, Integer> buyDishesMap) {
        mBuyDishesMap = buyDishesMap;
    }

    private void updateItem(final ViewHolder helper, final DishesEntry item) {
        if (mBuyDishesMap.containsKey(item) && mBuyDishesMap.get(item) > 0) {
            helper.showCalcEnable(true);
            helper.mBuyNumTextView.setText(mBuyDishesMap.get(item).toString());
        } else {
            helper.showCalcEnable(false);
        }
    }

    public OnNumChangeListener getOnNumChangeListener() {
        return mOnNumChangeListener;
    }

    public void setOnNumChangeListener(OnNumChangeListener onNumChangeListener) {
        mOnNumChangeListener = onNumChangeListener;
    }

    public interface OnNumChangeListener {
        void onAddNum(View v, DishesEntry dishesEntry, int position);

        void onReduceNum(View v, DishesEntry dishesEntry, int position);
    }

    public class ViewHolder extends BaseViewHolder {
        @BindView(R.id.tv_sticky_header_view)
        LinearLayout mTvStickyHeaderView;
        @BindView(R.id.dishesNameTextView)
        TextView mDishesNameTextView;
        @BindView(R.id.dishesPriceTextView)
        TextView mDishesPriceTextView;
        @BindView(R.id.minusButton)
        FrameLayout mMinusButton;
        @BindView(R.id.buyNumTextView)
        TextView mBuyNumTextView;
        @BindView(R.id.addButton)
        FrameLayout mAddButton;
        @BindView(R.id.stickyTextView)
        TextView mStickyTextView;

        public ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }

        public void showCalcEnable(boolean isEnable) {
            if (isEnable) {
                mMinusButton.setVisibility(View.VISIBLE);
                mBuyNumTextView.setVisibility(View.VISIBLE);
            } else {
                mMinusButton.setVisibility(View.INVISIBLE);
                mBuyNumTextView.setVisibility(View.INVISIBLE);
            }
        }
    }

    public static class Mode {
        public static final int NORMAL = 0;
        public static final int QUERY = 1;
    }

    @Retention(SOURCE)
    @IntDef({Mode.NORMAL, Mode.QUERY})
    public @interface ModeDef {
    }

}
